﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
//m3u8下载类
namespace WpfApp1.config
{
    internal class M3u8_Down
    {
        public void startdown(string url, string resOrder)
        {
            var title = resOrder.Replace(",", "").Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", "");
            string[] names = new string[] { "#OUT," + Directory.GetCurrentDirectory() + "\\Download", title + "," + url };
            using (StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + "\\m3u8\\1.txt"))
            {
                foreach (string s in names)
                {
                    sw.WriteLine(s);

                }
                sw.Close();
            }
            videodown.m3u8down("\"" + Directory.GetCurrentDirectory() + "\\m3u8\\m3u8.exe\"  1.txt");

        }

        public void startddd(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;

            var title = menuItem.Header.ToString().Split(";")[0]; 
            var uri = menuItem.Header.ToString().Split(";")[1];

            string[] names = new string[] { "#OUT," + Directory.GetCurrentDirectory() + "\\Download", title + "," + uri };
            using (StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + "\\m3u8\\1.txt"))
            {
                foreach (string s in names)
                {
                    sw.WriteLine(s);
                }
                sw.Close();
            }
            videodown.m3u8down("\"" + Directory.GetCurrentDirectory() + "\\m3u8\\m3u8.exe\"  1.txt");
        }
    }
    public class videodown
    {
        public static void m3u8down(string cmd)
        {
            Process myProcess = new Process();//创建进程对象
            myProcess.StartInfo.FileName = "cmd.exe";//设置打开cmd命令窗口
            myProcess.StartInfo.UseShellExecute = false;//不使用操作系统shell启动进程的值
            myProcess.StartInfo.RedirectStandardInput = true;//设置可以从标准输入流读取值
            myProcess.StartInfo.RedirectStandardOutput = true;//设置可以向标准输出流写入值
            myProcess.StartInfo.RedirectStandardError = true;//设置可以显示输入输出流中出现的错误
            myProcess.StartInfo.CreateNoWindow = true;//设置在新窗口中启动进程
            myProcess.Start();//启动进程
            myProcess.StandardInput.WriteLine(cmd);//传入要执行的命令
        }
    }
}
